﻿@page "/user/login"
@layout FrontLayout
@inject IAuthService AuthService
@inject NavigationManager NavigationManager
@inject IMessageService _message

<PageTitle>登录</PageTitle>

<GridRow Justify="center" Align="middle" Style="height: 90%;">
    <GridCol Span="7">
        <div class="register" style="background-color: white;border-radius: 4px;padding: 20px;">
            <h1 style="text-align: center;">用户登录</h1>
            <Form Loading="_loading" Model="@_user" LabelColSpan="6" WrapperColSpan="16">
                <FormItem Label="用户名">
                    <Input Placeholder="请输入用户名" @bind-Value="@context.UserName"></Input>
                </FormItem>
                <FormItem Label="密  码">
                    <Input Placeholder="请输入密码" @bind-Value="@context.Password" Type="password"></Input>
                </FormItem>

                <FormItem WrapperColOffset="8" WrapperColSpan="16" Style="margin-bottom: 0;">
                    <Button Type="primary" OnClick="SubmitLogin" class="submit">登录</Button>
                </FormItem>
            </Form>
        </div>

    </GridCol>
</GridRow>

@code {
    private bool _loading = false;

    private readonly LoginModel _user = new LoginModel();

    public async Task SubmitLogin()
    {
        _loading = true;
        var result = await AuthService.Login(_user);

        if (result.Successful)
        {
            NavigationManager.NavigateTo("/admin/welcome", true);
        }
        else
        {
            _loading = false;
            await _message.Error(result.Error, 2);
        }
    }
}
